package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCJobCurrVw;
import com.auxgroup.hr.front.dao.domain.PsCPersonalData;
import com.auxgroup.hr.front.dao.domain.PsCWfPrcHdr;
import com.auxgroup.hr.front.dao.repository.PsCJobCurrVwRepository;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.WfPrcHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfPrcHdrRespVO;
import com.auxgroup.hr.front.site.vo.process.WfPrcHdrSaveQueVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 转正流程CO
 *
 * @author wangchangyun
 * @version 创建时间：180527
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfprchdr")
public class WfPrcHdrCO extends BaseWfCO {

    private static final Logger logger = LoggerFactory.getLogger(WfPrcHdrCO.class);

    @Autowired
    public WfPrcHdrService wfPrcHdrService;
    @Autowired
    private PersonalDataService personalDataService;
    @Autowired
    private PsCJobCurrVwRepository psCJobCurrVwRepository;


    //需要控制的权限字段

    @GetMapping(value = "/getWfPrcHdr")
    public BaseResponseVO getWfPrcHdr(String cTransactionNbr, String stepNbr, @SessionGain("user") UserDTO user) {
        if (StringUtils.isBlank(cTransactionNbr)) {
            return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
        }
        PsCWfPrcHdr prcHdr = wfPrcHdrService.getWfPrcHdr(cTransactionNbr);
        if (prcHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }

        WfPrcHdrRespVO respVO = objectMapper.map(prcHdr, WfPrcHdrRespVO.class);
        //导师名字
        PsCPersonalData daoshi = personalDataService.getPersonalData(prcHdr.getcEmplid());
        if (daoshi != null) {
            respVO.setcEmplidName(daoshi.getName());
        }
        PsCPersonalData self = personalDataService.getPersonalData(prcHdr.getEmplid());
        respVO.setcGraEndDt(self.getcGraEndDt());
        respVO.setDeptName(sysDictService.getDeptDescr(prcHdr.getDeptid()));


        PsCJobCurrVw currWw = psCJobCurrVwRepository.findOne(new JobCurrPrimary(BigDecimal.ZERO, prcHdr.getEmplid()));
        if (currWw != null) {
            //事业部经理
            respVO.setUnitManager(sysDictService.getUnitManagerName(currWw.getSetidJobcode()));
            //部门总经理
            respVO.setDeptManager(sysDictService.getDeptManagerName(currWw.getDeptid()));
        }
//		respVO.setcPrcType();
        fillBaseWfInfo(user, prcHdr, stepNbr, respVO);
        return buildRespone(respVO);
    }


    /**
     * 修改流程信息
     *
     * @param requestVO
     * @param binding
     * @param user
     * @return
     */
    @PostMapping(value = "/savePrc")
    public BaseResponseVO saveWfPrcHdr(@Valid WfPrcHdrSaveQueVO requestVO, @SessionGain("user") UserDTO user, BindingResult binding) {
        logger.info("PrcHdr save param:{}", requestVO);
        // 表单校验
        super.assertValidation(binding);
        PsCWfPrcHdr prcHdr = wfPrcHdrService.getWfPrcHdr(requestVO.getcTransactionNbr());
        if (prcHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }
//        objectMapper.map(requestVO, prcHdr,"notCopyNull");

//		fillParams(prcHdr, user, requestVO);
        Set<String> fs = super.getEditableFields(prcHdr, user);
        if (fs.contains("cAchievement")) prcHdr.setcAchievement(requestVO.getcAchievement());
        if (fs.contains("cDefect")) prcHdr.setcDefect(requestVO.getcDefect());
        if (fs.contains("cPrcType")) prcHdr.setcPrcType(requestVO.getcPrcType());
        if (fs.contains("cPrcDt")) prcHdr.setcPrcDt(requestVO.getcPrcDt());
        if (fs.contains("cGroupType")) prcHdr.setcGroupType(requestVO.getcGroupType());
        wfPrcHdrService.save(prcHdr);
        return requestWs(WfTypeEnum.WF_STYPE_PRC.getType(), requestVO, user);
    }


    /**
     * 保存流程信息
     *
     * @param requestVO
     * @param user
     * @return
     */
    @PostMapping(value = "/saveWfPrcHdr")
    public BaseResponseVO saveBaseWfPrcHdr(@RequestBody WfPrcHdrSaveQueVO requestVO, @SessionGain("user") UserDTO user) {
        logger.info("PrcHdr save param:{}", requestVO);
        // 表单校验
        PsCWfPrcHdr prcHdr = wfPrcHdrService.getWfPrcHdr(requestVO.getcTransactionNbr());
        if (prcHdr == null) {
            return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
        }
//        objectMapper.map(requestVO, prcHdr,"notCopyNull");

//		fillParams(prcHdr, user, requestVO);
        Set<String> fs = super.getEditableFields(prcHdr, user);
        if (fs.contains("cAchievement")) prcHdr.setcAchievement(requestVO.getcAchievement());
        if (fs.contains("cDefect")) prcHdr.setcDefect(requestVO.getcDefect());
        if (fs.contains("cPrcType")) prcHdr.setcPrcType(requestVO.getcPrcType());
        if (fs.contains("cPrcDt")) prcHdr.setcPrcDt(requestVO.getcPrcDt());
        if (fs.contains("cGroupType")) prcHdr.setcGroupType(requestVO.getcGroupType());
        wfPrcHdrService.save(prcHdr);
        return buildRespone(prcHdr);
    }

    @Override
    protected Set<String> getAuthFileds() {
        return new HashSet<>(Arrays.asList("cAchievement", "cDefect", "cAssmRslt", "cPrcType", "cPrcDt", "cGroupType"));
    }
}
